/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameheart.client.logic;

import gameheart.client.sprite.CardSprite;
import gameheart.transport.MyException;
import gameheart.server.ServerCard;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author win7
 */
public class HandState extends PlayState {

    ArrayList _players[] = new ArrayList[4];
    CardSprite _centerCards[] = new CardSprite[4];

    public HandState(PlayCanvas context) {
        super(context);
        for (int index = 0; index < _players.length; ++index) {
            _players[index] = new ArrayList();
        }

    }
    boolean _isRoundtripEnd = false;

    @Override
    public void update() {
        System.out.println("hand state.");
        if(_context._mainGame.getAdapter().isHandEnd() && _context._mainGame.getAdapter().IsHaveDealRequest())
        {
            boolean isMoving = false;
            for(int index = 0; index < 4; ++index)
            {
                if(_centerCards[index] != null && _centerCards[index].isMoving())
                {
                    isMoving = true;
                    break;
                }
            }
            if(!isMoving)
            {
                _context.updateMark();
                _context._mainGame.getAdapter().beginNewHand();
                _context.changeState(playStateType.DealingState);
            }
        }
        for (int index = 0; index < _centerCards.length; ++index) {
            if (_centerCards[index] != null) {
                _centerCards[index].updatePosition();
            }
        }
        if (_context._mainGame.getAdapter().IsRoundTripEnd()) {
            roundTripEnd();
        }
        if (_context._mainGame.getAdapter().IsHavePlayRequest() && !_isRoundtripEnd) {
            waitingUserPlay();
        }
        if (_context._mainGame.getAdapter().IsHaveDrawMovingCard()) {
            DrawPlayCard();
        }
    }

    private void roundTripEnd() {
        int playerIndex = (_context._mainGame.getAdapter().getIdOfGetCardUser() + 4 - _context._mainGame.getAdapter().getCurrentUserId()) % 4;
        if (!_isRoundtripEnd) {
            for (int index = 0; index < _centerCards.length; ++index) {
                int x = _context._beginLocationOfPlayers[playerIndex].x + 6 * _context._deltaVector[playerIndex].x;
                int y = _context._beginLocationOfPlayers[playerIndex].y + 6 * _context._deltaVector[playerIndex].y;
                _centerCards[index].move(x, y);
            }
            _isRoundtripEnd = true;
        } else {
            boolean finishMoving = true;
            for (int index = 0; index < 4; ++index) {
                if (_centerCards[index].isMoving()) {
                    finishMoving = false;
                }
            }
            if (finishMoving) {
                _context._mainGame.getAdapter().finishRoundTripDraw();
                _isRoundtripEnd = false;
                for (int index = 0; index < _centerCards.length; ++index) {
                    _centerCards[index] = null;
                }
                _context._clickedPosition.setRect(null);
            }
        }
    }

    private void DrawPlayCard() {
        int playerIndex = (_context._mainGame.getAdapter().getPlayedUserId() + 4 - _context._mainGame.getAdapter().getCurrentUserId()) % 4;
        if (_centerCards[playerIndex] == null) {
            if (playerIndex != 0) {
                CardSprite currentCard = (CardSprite) _players[playerIndex].get(_players[playerIndex].size() - 1);
                CardSprite playedCard = new CardSprite(currentCard.getX(), currentCard.getY(), _context._mainGame.getAdapter().getCard(), _context._imageResposity);
                _players[playerIndex].remove(_players[playerIndex].size() - 1);
                _centerCards[playerIndex] = playedCard;
                _centerCards[playerIndex].move(_context._centerPointOfPlayer[playerIndex].x, _context._centerPointOfPlayer[playerIndex].y);
            } else {
                for (int cardIndex = 0; cardIndex < _players[playerIndex].size(); ++cardIndex) {
                    CardSprite sprite = (CardSprite) _players[playerIndex].get(cardIndex);
                    if (sprite._content.compareTo(_context._mainGame.getAdapter().getCard()) == 0) {
                        _centerCards[playerIndex] = sprite;
                        _players[playerIndex].remove(sprite);
                        sprite.move(_context._centerPointOfPlayer[playerIndex].x, _context._centerPointOfPlayer[playerIndex].y);
                        recaculatePositions();
                        break;
                    }
                }
            }
        } else {
            if (!_centerCards[playerIndex].isMoving()) {
                try {
                    _context._mainGame.getAdapter().SendDrawedReply();
                } catch (MyException ex) {
                    Logger.getLogger(HandState.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    boolean firstCard = true;
    boolean candPlayCo = false;

    private void waitingUserPlay() {
        recaculatePositions();
        Point mousePoint = _context.getMousePoint();
        if (mousePoint != null) {
            Rectangle rect = new Rectangle(mousePoint.x, mousePoint.y, 1, 1);
            for (int index = _players[0].size() - 1; index >= 0; --index) {
                CardSprite sprite = (CardSprite) _players[0].get(index);
                if (sprite.isIntersec(rect)) {
                    sprite.setY(sprite.getY() - 30);
                    break;
                }
            }
        }
        if (_context._clickedPosition.getRect() != null && mousePoint != null) {
            Rectangle currentMouseRect = new Rectangle(mousePoint.x, mousePoint.y, 3, 3);
            Rectangle clickedRect = _context._clickedPosition.getRect();
            if (currentMouseRect.intersects(clickedRect)) {
                for (int index = 0; index < _players[0].size(); ++index) {
                    CardSprite card = (CardSprite) _players[0].get(index);
                    if (card.isIntersec(clickedRect)) {
                        if (card.getY() < _context._beginLocationOfPlayers[0].y - 5) {
                            try {
                                if (_context._mainGame.getAdapter().SendPlayReply(card._content)) {
                                    System.out.print("Vua thuc hien nuoc di");
                                }
                            } catch (MyException ex) {
                                Logger.getLogger(HandState.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }

                    }
                }
            }
        }
    }

    @Override
    public void render(Graphics graphic) {
        for (int playerIndex = 0; playerIndex < _players.length; ++playerIndex) {
            for (int cardIndex = 0; cardIndex < _players[playerIndex].size(); ++cardIndex) {
                CardSprite sprite = (CardSprite) _players[playerIndex].get(cardIndex);
                sprite.draw(graphic);
            }
        }
        for (int index = 0; index < _centerCards.length; ++index) {
            if (_centerCards[index] != null) {
                _centerCards[index].draw(graphic);
            }
        }
    }

    @Override
    public playStateType getStateType() {
        return playStateType.HandState;
    }

    @Override
    public PlayState Clone() {
        HandState clonedResult = new HandState(_context);
        return clonedResult;
    }

    @Override
    public void Init() {
        Point delta = _context._deltaVector[0];
        for (int index = 0; index < 13; ++index) {
            Point beginLocation = _context._beginLocationOfPlayers[0];
            int x = beginLocation.x + delta.x * index;
            int y = beginLocation.y + delta.y * index;

            ServerCard[] myCards = _context._mainGame.getAdapter().getMyCards();
            _players[0].add(new CardSprite(x, y, myCards[index], _context._imageResposity));
        }
        for (int playerIndex = 1; playerIndex < _players.length; ++playerIndex) {
            delta = _context._deltaVector[playerIndex];
            for (int cardIndex = 0; cardIndex < 13; ++cardIndex) {
                Point beginLocation = _context._beginLocationOfPlayers[playerIndex];
                int x = beginLocation.x + delta.x * cardIndex;
                int y = beginLocation.y + delta.y * cardIndex;


                _players[playerIndex].add(new CardSprite(x, y, null, _context._imageResposity));
            }
        }
    }

    private void recaculatePositions() {
        Point delta = _context._deltaVector[0];
        for (int cardIndex = 0; cardIndex < _players[0].size(); ++cardIndex) {
            Point beginLocation = _context._beginLocationOfPlayers[0];
            int x = beginLocation.x + delta.x * cardIndex;
            int y = beginLocation.y + delta.y * cardIndex;
            CardSprite sprite = (CardSprite) _players[0].get(cardIndex);
            sprite.setX(x);
            sprite.setY(y);
        }
    }
}
